﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using Runda.CMS.Bll;
using Runda.CMS.Common.Utility;
using Runda.CMS.Common.Entity;

namespace Runda.CMS.Portal.SyncManage
{
    public partial class AccessoryInInstrumentAdd : BasePage
    {
        public Guid InstrumentID
        {
            get
            {
                if (hiddenInstrumentID.Value == string.Empty) return Guid.Empty;
                else return new Guid(hiddenInstrumentID.Value);
            }
            set
            {
                hiddenInstrumentID.Value = value.ToString();
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack) {

                BrandList.BindDDDW(new CommonBll().dddw("vendor"), "vendor", "vendor");

                if (Request.QueryString["instrumentID"] != null) { 
                    WsmInstrumentInfoEntity entity = new SystemBll().Get<WsmInstrumentInfoEntity>(new Guid(Request.QueryString["instrumentID"]));
                    if (entity != null)
                    {
                        InstrumentID = entity.InstrumentID;
                        BrandList.SelectedValue = entity.Vendor;
                        lbInstrumentName.Text = entity.instrumentName;
                        refreshAccessoryList();
                    }
                }
            }
        }
        private void save()
        {
            for (int i = 0; i < gridSubItems.Items.Count; i++)
            {
                Label AccessoryID = gridSubItems.Items[i].FindControl("AccessoryID") as Label;
                
                List<WsmAccessoryInInstrumentEntity> list = new SystemBll().GetAll<WsmAccessoryInInstrumentEntity>(" and InstrumentID = '" + InstrumentID.ToString() + "' and AccessoryID = '" + AccessoryID.Text + "'");
                if (list != null && list.Count > 0) {
                    continue;
                }

                WsmAccessoryInInstrumentEntity entity = new WsmAccessoryInInstrumentEntity();
                entity.ID = Guid.NewGuid();
                entity.InstrumentID = InstrumentID;
                entity.AccessoryID = new Guid(AccessoryID.Text);
                entity.Deleted = false;
                new SystemBll().InsertOrUpdate(entity);
            }
            ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功');window.opener=null;window.open('','_self');window.close();", true);
        }
          

        protected void lnkbtnSaveAndClose_Click(object sender, EventArgs e)
        {
            save();
            ClientScript.RegisterStartupScript(this.GetType(), "save", "alert('保存成功');window.close();", true);
        }

        protected void BrandList_SelectedIndexChanged(object sender, EventArgs e)
        {
            refreshAccessoryList();
        }

        protected void ddlAccessoryType_SelectedIndexChanged(object sender, EventArgs e)
        {
            refreshAccessoryList();
        }

        private void refreshAccessoryList()
        {
            string sqlWhereClause = "";
            if (BrandList.SelectedValue != "" && BrandList.SelectedValue != null)
            { sqlWhereClause += " and Vendor = '" + BrandList.SelectedValue + "'"; }

            if (ddlAccessoryType.SelectedValue != "" && ddlAccessoryType.SelectedValue != null)
            { sqlWhereClause += " and AccessoryType = " + ddlAccessoryType.SelectedValue + ""; }

            AccessoryList.Items.Clear();
            List<WsmAccessoryInfoEntity> list = new SystemBll().GetAll<WsmAccessoryInfoEntity>(sqlWhereClause, " order by CompanyInstrumentCode asc ");
            foreach (WsmAccessoryInfoEntity accessentity in list)
            {
                AccessoryList.Items.Add(new ListItem(accessentity.CompanyInstrumentCode + '-' + accessentity.AccessoryName, accessentity.AccessoryID.ToString()));
            }
        }

        private DataTable getDateFromGrid(int i)
        {
            //DataTable dt = new AccessoryInfo().getAccessoryInfoStructure();
            DataTable dt = new CommonBll().getDataFromSQL("select * from Cms_Wsm_AccessoryInfo where 1<>1");
            for (int index = 0; index < gridSubItems.Items.Count; index++)
            {
                Label AccessoryID = gridSubItems.Items[index].FindControl("AccessoryID") as Label;
                Label AccessoryType = gridSubItems.Items[index].FindControl("AccessoryType") as Label;
                Label AccessoryTypeName = gridSubItems.Items[index].FindControl("AccessoryTypeName") as Label;
                Label AccessoryName = gridSubItems.Items[index].FindControl("AccessoryName") as Label;
                Label CompanyInstrumentCode = gridSubItems.Items[index].FindControl("CompanyInstrumentCode") as Label;
                Label FactoryInstrumentCode = gridSubItems.Items[index].FindControl("FactoryInstrumentCode") as Label;
                Label AccessoryStandard = gridSubItems.Items[index].FindControl("AccessoryStandard") as Label;
                Label Unit = gridSubItems.Items[index].FindControl("Unit") as Label;
                Label Vendor = gridSubItems.Items[index].FindControl("Vendor") as Label;

                if (index != i)
                {
                    DataRow dr = dt.NewRow();
                    dr["AccessoryID"] = AccessoryID.Text;
                    dr["AccessoryName"] = AccessoryName.Text;
                    dr["AccessoryType"] = AccessoryType.Text;
                    dr["CompanyInstrumentCode"] = CompanyInstrumentCode.Text;
                    dr["FactoryInstrumentCode"] = FactoryInstrumentCode.Text;
                    dr["AccessoryStandard"] = AccessoryStandard.Text;
                    dr["Unit"] = Unit.Text;
                    dr["Vendor"] = Vendor.Text;
                    dt.Rows.Add(dr);
                }
            }
            return dt;


        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            DataTable dt = getDateFromGrid(-1);
            bool isExist = false;
            for (int index = 0; index < dt.Rows.Count; index++)
            {
                String AccessoryId = dt.Rows[index]["AccessoryID"].ToString();
                if (AccessoryId ==  AccessoryList.SelectedValue.ToString())
                {
                    isExist = true;
                }
            }
            if (!isExist)
            {
                DataRow dr = dt.NewRow();
                if (AccessoryList.SelectedValue.ToString() == "") return;
                dr["AccessoryID"] = AccessoryList.SelectedValue.ToString();
                WsmAccessoryInfoEntity entity = new SystemBll().Get<WsmAccessoryInfoEntity>(new Guid(AccessoryList.SelectedValue.ToString()));
                dr["AccessoryType"] = entity.AccessoryType;
                dr["AccessoryName"] = entity.AccessoryName;
                dr["CompanyInstrumentCode"] = entity.CompanyInstrumentCode;
                dr["FactoryInstrumentCode"] = entity.FactoryInstrumentCode;
                dr["AccessoryStandard"] = entity.AccessoryStandard;
                dr["Unit"] = entity.Unit;
                dr["Vendor"] = entity.Vendor;
                dt.Rows.Add(dr);
            }
            gridSubItems.DataSource = dt;
            gridSubItems.DataBind();
        }

        protected void gridSubItems_ItemCommand(object source, Telerik.WebControls.GridCommandEventArgs e)
        {
            if (e.CommandName == "delete")
            {
                gridSubItems.DataSource = getDateFromGrid(e.Item.ItemIndex);
                gridSubItems.DataBind();
            }
        }

        protected void gridSubItems_ItemDataBound(object sender, Telerik.WebControls.GridItemEventArgs e)
        {
            if (e.Item.ItemType == Telerik.WebControls.GridItemType.Item || e.Item.ItemType == Telerik.WebControls.GridItemType.AlternatingItem)
            {
                Label AccessoryType = e.Item.FindControl("AccessoryType") as Label;
                Label AccessoryTypeName = e.Item.FindControl("AccessoryTypeName") as Label;

                AccessoryTypeName.Text = AccessoryType.Text == "1" ? "配件" : "易耗件";


            }
        }
    }
}
